<?php
if (!defined('BASEPATH')) exit('No direct access allowed.');
require_once dirname(dirname(__FILE__)) . '/Pay.php';
class paypal extends Pay {

    protected $gateway = 'https://www.paypal.com/cgi-bin/webscr';//'https://www.sandbox.paypal.com/cgi-bin/webscr';
    protected $formData = array();
    protected $config = array(
        'business' => '',
        
    );

    public function check() {
        if (!$this->config['business']) {
            
        }
        return true;
    }

    public function buildRequestForm() {
        $param = array(
            'cmd' => '_xclick',
            'charset' => 'utf-8',
            'business' => $this->config['business'],
            'currency_code' => $this->config['currency_code'],
            'notify_url' => $this->config['notify_url'],
            'return' => $this->config['return_url'],
            'invoice' =>$this->formData['invoice_no'],
            'item_name' => $this->formData['title'],
            'amount' => (float)$this->formData['total'],
            'no_note' => 1,
            'no_shipping' => 1
        );
        $sHtml = $this->_buildForm($param, $this->gateway);

        return $sHtml;
    }
    public function setFormData($data){
        $this->formData = $data;
    }
    public function verifyNotify($notify) {
        if (empty($notify['txn_id']))
            return false;
        $tmpAr = array_merge($notify, array("cmd" => "_notify-validate"));
        $ppResponseAr = $this->fsockOpen($this->gateway, 0, $tmpAr);
        if ((strcmp($ppResponseAr, "VERIFIED") == 0) && $notify['receiver_email'] == $this->config['business']) {
            $info = array();
            //支付状态
            $info['status'] = $notify['payment_status'] == 'Completed' ? true : false;
            $info['money'] = $notify['mc_gross'];
            $info['out_trade_no'] = $notify['invoice'];
            $this->info = $info;
            return true;
        }
        return false;
    }
    /*获取到通知订单详细*/
   public function getNotify($tx){
       //$authToken = $this->config['authtoken'];
       $req = 'cmd=_notify-synch';// 验证请求
       $tx_token = $tx;
       $auth_token = $this->config['authtoken'];
       $req .= "&tx=$tx_token&at=$auth_token";
       $ch = curl_init();
       curl_setopt($ch,CURLOPT_URL,$this->gateway);
       curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
       curl_setopt($ch,CURLOPT_POST,1);
       curl_setopt($ch,CURLOPT_POSTFIELDS,$req);
       $res = curl_exec($ch);
       curl_close($ch);
       $arr  = explode("\n",$res);
       if(strcmp($arr[0],"SUCCESS") == 0){
           $data = array();
          foreach($arr as $k=>$v){
              list($key,$value) = explode("=",$v);
              $data[$key] = $value;
          }
           $info['status'] = $data['payment_status'] == 'Completed' ? true : false;
           $info['money'] = $data['mc_gross'];
           $info['out_trade_no'] = $data['invoice'];
           $info['cc'] =$data['mc_currency'];
           $info['payer_email'] = urldecode($data['payer_email']);
           $info['payer_name'] = urldecode($data['first_name']).urldecode($data['Yip']);
          return $info;
       }
       return false;

   }
}
